package gen.core.managers;

import gen.core.exceptions.GenException;
import gen.core.models.ConnConfig;
import gen.core.others.GenBeanProcess;

import java.sql.Connection;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

public class ConnConfigManager {
	public ConnConfig[] list(Connection conn, String owner, String catagory) {
		String sql;
		QueryRunner runner = new QueryRunner();

		BeanListHandler<ConnConfig> handler = new BeanListHandler<ConnConfig>(ConnConfig.class, new BasicRowProcessor(new GenBeanProcess()));
		sql = "SELECT * FROM GEN_CONN_CONFIG WHERE OWNER = ? AND CATAGORY = ?";
		try {
			List<ConnConfig> aList = runner.query(conn, sql, handler, new Object[] { owner, catagory });
			return aList.toArray(new ConnConfig[0]);
		} catch (Exception ex) {
			throw new GenException(ex, "读取数据库连接列表时，发生异常！");
		}
	}

	public ConnConfig load(Connection conn, String id) {
		String sql;
		QueryRunner runner = new QueryRunner();

		BeanHandler<ConnConfig> handler = new BeanHandler<ConnConfig>(ConnConfig.class, new BasicRowProcessor(new GenBeanProcess()));
		sql = "SELECT * FROM GEN_CONN_CONFIG WHERE ID = ?";
		try {
			return runner.query(conn, sql, handler, new Object[] { id });
		} catch (Exception ex) {
			throw new GenException(ex, "读取数据库连接时，发生异常！连接ID=" + id + "！");
		}
	}

	public void save(Connection conn, ConnConfig theConnConfig) {
		String sql;
		QueryRunner runner = new QueryRunner();

		try {
			if (StringUtils.isBlank(theConnConfig.getId())) {
				sql = "INSERT INTO GEN_CONN_CONFIG VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
				theConnConfig.setId(UUID.randomUUID().toString());
				theConnConfig.setCreateTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
				theConnConfig.setModifiedTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
				runner.update(conn, sql, theConnConfig.getId(), theConnConfig.getName(), theConnConfig.getDescription(), theConnConfig.getCatagory(),
						theConnConfig.getDriver(), theConnConfig.getUrl(), theConnConfig.getUser(), theConnConfig.getPassword(), theConnConfig.getOwner(),
						theConnConfig.getCreateTime(), theConnConfig.getModifiedTime());
			} else {
				theConnConfig.setModifiedTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
				sql = "UPDATE GEN_CONN_CONFIG SET NAME=?, DESCRIPTION=?, CATAGORY=?, DRIVER=?, URL=?, USER=?, PASSWORD=?, MODIFIED_TIME=? WHERE ID = ?";
				runner.update(conn, sql, theConnConfig.getName(), theConnConfig.getDescription(), theConnConfig.getCatagory(), theConnConfig.getDriver(),
						theConnConfig.getUrl(), theConnConfig.getUser(), theConnConfig.getPassword(), theConnConfig.getModifiedTime(), theConnConfig.getId());
			}
		} catch (Exception ex) {
			throw new GenException(ex, "保存数据库连接时，发生异常！");
		}
	}

	public void delete(Connection conn, String id) {
		String sql;
		QueryRunner runner = new QueryRunner();

		sql = "DELETE FROM GEN_CONN_CONFIG WHERE ID = ?";
		try {
			runner.update(conn, sql, id);
		} catch (Exception ex) {
			throw new GenException(ex, "删除数据库连接时，发生异常！连接ID=" + id + "！");
		}
	}
}
